//
//  VideoViewController.m
//  B_WatchHome
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 DHF. All rights reserved.
//

#import "VideoViewController.h"
#import "AppTools.h"
#import "ViedoCollectionViewCell.h"
#import "VideoModel.h"
#import "VideoDataViewController.h"
#import "VideoCollectionReusableView.h"
#import <MJRefresh.h>
#import "MagazineDataViewController.h"
#import <UIImage+GIF.h>

#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface VideoViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, jumpDelegate>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)VideoCollectionReusableView *reView;

@end

@implementation VideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.sectionInset = UIEdgeInsetsMake((10*WIDTH)/375, (10*HEIGHT)/667, (20*WIDTH)/375, (10*HEIGHT)/667);
    
    flowLayout.itemSize = CGSizeMake((172*WIDTH)/375, (178*HEIGHT)/667);
    
    flowLayout.minimumLineSpacing = (30*HEIGHT)/667;
    
    //设置最小列间距
    flowLayout.minimumInteritemSpacing = (20*WIDTH)/375;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
    // 隐藏滚动条
    [self.collectionView setShowsVerticalScrollIndicator:NO];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    self.collectionView.backgroundColor = [UIColor lightGrayColor];
    
//    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[ViedoCollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell_pe"];
    
    [self.collectionView registerClass:[VideoCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionReusableView"];
    
    self.navigationItem.title = @"WATCHVIDEOS";
    
    self.keysArray = [NSMutableArray array];
    
    self.i = 1;
    
    self.bigArray = [NSMutableArray array];
    
    [self getAFNetWorkingData];
    
    // 上下拉刷新
    [self addFooterRefresh];
    
//    [self addHeaderRefresh];
    
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
    NSString *name = @"header.gif";
    NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    UIImage *image = [UIImage sd_animatedGIFWithData:imageData];
    
    NSArray *arr = [NSArray arrayWithObjects:image, nil];
    
    [header setImages:arr forState:MJRefreshStateIdle];
    
    [header setImages:arr forState:MJRefreshStatePulling];
    
    [header setImages:arr forState:MJRefreshStateRefreshing];
    
    self.collectionView.mj_header = header;
    
    
}

- (void)loadNewData
{
    NSLog(@"下拉");
    
    self.i = 1;
    
    //  重新请求数据
    [self getAFNetWorkingData];
    
    [self.collectionView.mj_header beginRefreshing];
}


//- (void)addHeaderRefresh
//{
//    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//       
//        NSLog(@"下拉");
//        self.i = 1;
//        [self getAFNetWorkingData];
//       
//    }];
//    [self.collectionView.mj_header beginRefreshing];
//    
//}

- (void)addFooterRefresh
{
    self.collectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
       
        NSLog(@"上拉");
        
        self.i++;
        
        [self getAFNetWorkingData];
        
    }];
}


// 网络请求
- (void)getAFNetWorkingData
{
    NSString *brandUrl = [NSString stringWithFormat:@"http://watch-cdn.idailywatch.com/api/list/video/zh-hans?page=%ld&ver=iphone&app_ver=10" ,self.i];
    
    if (self.i == 1) {
        [AppTools getDataWithURL:brandUrl Block:^(id result) {
           
            [self.bigArray removeAllObjects];
            [self.keysArray removeAllObjects];
            
            NSMutableArray *poArray = result;
            for (NSMutableDictionary *dic in poArray) {
                VideoModel *model = [[VideoModel alloc] init];
                
                [model setValuesForKeysWithDictionary:dic];
                if (model.guid != 15957) {
                    [self.bigArray addObject:model];
                }else{
                
                    [self.keysArray addObject:model];
                    
                }
                
            }
            
            [self.collectionView reloadData];
            
            // 上下拉结束
            [self.collectionView.mj_header endRefreshing];
            [self.collectionView.mj_footer endRefreshing];
            
            
        }];
    }else{
    
        [AppTools getDataWithURL:brandUrl Block:^(id result) {
           
            NSMutableArray *poArray = result;
            
            for (NSMutableDictionary *dic in poArray) {
                VideoModel *model = [[VideoModel alloc] init];
                
                [model setValuesForKeysWithDictionary:dic];
                
                if (model.guid != 15957) {
                    [self.bigArray addObject:model];
                }else{
                    [self.keysArray addObject:model];
                }
            }
            
            [self.collectionView reloadData];
            
            // 上下拉结束
            [self.collectionView.mj_header endRefreshing];
            [self.collectionView.mj_footer endRefreshing];
            
        }];
    }
}

// 代理方法没写
- (void)presentToViewController:(VideoModel *)model
{
    self.detailVC = [[VideoDataViewController alloc] init];
    
    self.detailVC.model = model;
    
    [self presentViewController:self.detailVC animated:YES completion:nil];

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    return CGSizeMake(([UIScreen mainScreen].bounds.size.width*WIDTH)/375, (200*HEIGHT)/667);
}


// 头没写

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        VideoCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CollectionReusableView" forIndexPath:indexPath];
        
        header.jump = self;
        
        return header;
    }
    return nil;
}




- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.bigArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"CollectionViewCell_pe";
    
    ViedoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
    VideoModel *model = [self.bigArray objectAtIndex:indexPath.item];
    
    cell.model = model;
    
    return cell;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    
    return (5*WIDTH)/375;
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    VideoModel *model = [self.bigArray objectAtIndex:indexPath.item];
    
    VideoDataViewController *detailVC = [[VideoDataViewController alloc]init];
    
    detailVC.model = model;
    
    [self presentViewController:detailVC animated:YES completion:nil];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
